package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜品实现类
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;



    @Override
    @Transactional
    public void saveDishAndFlavor(DishDTO dto) {

        //这里需要对两张表进行操作 所以应该开启事务 注意事务配置
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        //1.调用相关mapper 增加菜品表的数据
        dishMapper.insertDish(dish);
        //获取菜品id sql中注意返回id 下面才能获取得到
        Long id = dish.getId();
        //2.调用相关mapper 增加口味表的数据
        //这里涉及到批量添加 所以考虑动态sql
        List<DishFlavor> flavors = dto.getFlavors();
        //先判断必须要有口味才能添加
        //添加时 因为要添加菜品的id 所以上面得返回添加的菜品id
        if(flavors != null && flavors.size()>0 ) {
            //给每一个口味添加菜品id
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
           // log.info("更新后的列表{}",flavors);
            flavorMapper.insertFlavorBatch(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO>  page = dishMapper.pageQuery(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //1.已启用的菜品不能删除
        for (Long id :ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //2.关联有套餐的菜品不能删除
        List list = setmealDishMapper.getSetmealIdBydishID(ids);
        if(list != null && list.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //4.删除口味
            flavorMapper.deleteBydishID(id);
        }

    }

    @Override
    public void stopAndStart(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        //更改
        dishMapper.update(dish);
    }

    /**
     * 获取菜品的信息和口味信息
     * @param dishId
     * @return
     */
    @Override
    public DishVO getDishFlavorByID(Long dishId) {
        DishVO dishVO = new DishVO();
        //获取菜品信息
        Dish dish = dishMapper.getById(dishId);
        BeanUtils.copyProperties(dish,dishVO);
        //获取口味信息
        List<DishFlavor> dishFlavors = flavorMapper.getFlavorByDishId(dishId);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 更新菜品的信息以及口味信息
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateDishAndFlavor(DishDTO dishDTO) {
        //向菜品表插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //向口味表添加数据
        //1.删除原来的口味数据
        flavorMapper.deleteBydishID(dishDTO.getId());
        //2.添加新的口味数据
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        dishFlavors.forEach(flavor->{
            flavor.setDishId(dishDTO.getId()); //遍历并设置id
        });
        flavorMapper.insertFlavorBatch(dishDTO.getFlavors());
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     *  根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> dishListByCategoryID(Long categoryId) {
        List<Dish> list = dishMapper.dishListByCategoryID(categoryId);
        return list;
    }

}
